package kr.ac.skhu.m.validator;

import java.net.URL;
import java.util.List;

import kr.ac.skhu.m.value.BookData;
import kr.ac.skhu.m.value.BookId;

import net.htmlparser.jericho.Element;
import net.htmlparser.jericho.Source;

import org.springframework.validation.Errors;
import org.springframework.validation.Validator;

public class RentalYnValidator implements Validator {

	@Override
	public boolean supports(Class clazz) {
		if(BookId.class.isAssignableFrom(clazz)){
			return true;
		}
		return false;
	}
	
	@Override
	public void validate(Object target, Errors errors) {
		BookId bookId = (BookId)target;
		
		String book_id = bookId.getBookId();
		String rentalYn = "";

		try{
		
			//대여여부
			String detailUrl = "http://library.skhu.ac.kr/DLiWeb20/components/searchir/detail/detail.aspx?cid=" + book_id;
			Source detailSource = new Source(new URL(detailUrl));
			detailSource.fullSequentialParse();
			List<Element> td_white_left = detailSource.getAllElementsByClass("td_white_left");
			
			rentalYn = td_white_left.get(1).getTextExtractor().toString();
		
		}catch(Exception e){
			rentalYn = "";
		}
		
		System.out.println(rentalYn);
		errors.rejectValue("bookId", "rentalYn", rentalYn);
		
	}
	
}
